package com.dlwx.parttimetalent.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.base.BaseActivity;
import com.dlwx.parttimetalent.bean.BaseBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.comm.Http;
import com.dlwx.parttimetalent.utils.CheckUtil;
import com.dlwx.parttimetalent.utils.DealResult;
import com.dlwx.parttimetalent.utils.OkgoUtil;
import com.dlwx.parttimetalent.utils.TimeCount;
import com.dlwx.parttimetalent.utils.ToastUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PhoneActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.et_reg_phone)
    EditText etRegPhone;
    @BindView(R.id.et_yzm)
    EditText etYzm;
    @BindView(R.id.tv_get_yzm)
    TextView tvGetYzm;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        tvMiddleTitle.setText("手机认证");
        tvMiddleTitle.setVisibility(View.VISIBLE);
        titleLeftOneBtn.setVisibility(View.VISIBLE);
    }

    @OnClick({R.id.title_left_one_btn, R.id.tv_get_yzm, R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.tv_get_yzm:
                getYzm();
                break;
            case R.id.tv_confirm:
                checkInputDatas();

                break;
        }
    }

    private void getYzm() {
        String phone=etRegPhone.getText().toString();
        if(TextUtils.isEmpty(phone)|| !CheckUtil.isMobileNO(phone)){
            ToastUtils.toastForShort(this,"请输入正确手机号码");
            return;
        }
        OkgoUtil.post(Http.YZM,this)
                .params("phone_no",phone)
                .params("status",1)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        if(response==null){return;}
                        String res=response.body().toString();
                        BaseBean bean= new Gson().fromJson(res, BaseBean.class);
                        if(bean==null){return;};
                        if(bean.getCode()== Constant.SUCESS){
                            TimeCount timeCount = new TimeCount(tvGetYzm, 2 * 60 * 1000, 1000,"#ffffff");
                            timeCount.start();
                        }
                        ToastUtils.toastForShort(PhoneActivity.this,bean.getResult());
                    }
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        ToastUtils.toastForShort(PhoneActivity.this,getString(R.string.bad_net));
                    }
                });

    }

    private void checkInputDatas() {
        String phone=etRegPhone.getText().toString();
        String yzm=etYzm.getText().toString();
        if(TextUtils.isEmpty(phone)|| !CheckUtil.isMobileNO(phone)){
            ToastUtils.toastForShort(this,"请输入正确手机号码");
            return;
        }
        if(TextUtils.isEmpty(yzm)){
            ToastUtils.toastForShort(this,"密码不能为空");
            return;
        }
        confirm(phone,yzm);
    }

    private void confirm(String phone, String yzm) {
        loadingDialog.show();
        OkgoUtil.post(Http.UpdatePhone, this)
                .params("phone_no",phone)
                .params("codeno",yzm)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        BaseBean parentBean = DealResult.getInstance().dealDatas(PhoneActivity.this, response, new TypeToken<BaseBean>() {}.getType());
                        if (parentBean == null) {
                            return;
                        }
                        if(parentBean.getCode()==Constant.SUCESS){
                            PhoneActivity.this.finish();
                        }
                            ToastUtils.toastForShort(PhoneActivity.this, parentBean.getResult());
                    }
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDialog.dismiss();
                        ToastUtils.toastForShort(PhoneActivity.this,getString(R.string.bad_net));
                    }
                });
    }
}
